<?php
namespace app\manage\controller;

class Bag extends Base
{
    //礼包列表
    public function index(){
        if(request()->isAjax()){
            $page = input('page');
            $limit = input('limit');
            $id = input('id');
            $gameId = input('game_id');

            $where['status'] = ['<>', -1];
            !empty($id) && $where['id'] = $id;
            !empty($gameId) && $where['game_id'] = $gameId;

            if( ! $count=model('Bag')->where($where)->count() ){
               $this->layTable();
            }

            $select = model('Bag')
                ->where($where)
                ->limit(($page-1)*$limit, $limit)
                ->order('id desc')
                ->select();
            $data = collection($select)->append(['game_name']);

            $this->layTable(0, '礼包列表', $data, $count);
        }else{
            return view();
        }
    }

    //添加
    public function add(){
        if( request()->isAjax() ){
            $data = input();
            if($data['add_type']==1 && $data['file']){ //文件上传
                $data['file_name'] = $data['file'];
                unset($data['one_file']);
                unset($data['file']);

                $filePath = ROOT_PATH.'uploads/bag/user/'.input('game_id').'/';
                import('cut.Io');
                $Io = new \Io( $filePath );
                $data['total_num'] = $data['keep_num'] = $Io->fileCountLines( $filePath . $data['file_name'] );
            }else if($data['add_type']==2 && $data['one_file']){ //单个礼包码
                $data['file_name'] = $data['one_file'];
                unset($data['one_file']);
                unset($data['file']);

                $data['total_num'] = $data['keep_num'] = 99999;
            }else{
                $this->layJson(1, '请上传礼包码');
            }

            //游戏ID找区服ID
            $gameIdServer = changeValKey('ViewGameManage', 'game_id', 'app_server_id');
            $data['app_server_id'] = $gameIdServer[ $data['game_id'] ];

            $addRs = model('Bag')->save( $data );
            $addRs ? $this->layJson(0, '添加成功') : $this->layJson(1, '添加失败，请重试');
        }else{
            return view();
        }
    }

    //编辑
    public function edit(){
        if( request()->isAjax() ){
            $data = input();
            $editRs = model('Bag')->isUpdate(true)->save( $data );
            $editRs!==false ? $this->layJson(0, '编辑成功') : $this->layJson(1, '编辑失败，请重试');
        }else{
            $find = model('Bag')->find( input('id') );
            return view('', ['find'=>$find]);
        }
    }

    //删除
    public function del(){
        $data['id'] = input('id');
        $data['status'] = -1;
        $delRs = model('Bag')->isUpdate(true)->save( $data );
        $delRs!==false ? $this->layJson(0, '删除成功') : $this->layJson(1, '删除失败，请重试');
    }

    //重上传
//    public function again(){
//        if( request()->isAjax() ){
//            $data = input();
//            if(empty($data['file'])){
//                $this->layJson(1, '请上传文件');
//            }else{
//                $data['file_name'] = $data['file'];
//                unset($data['file']);
//            }
//
//            $filePath = ROOT_PATH.'uploads/bag/user/'.input('game_id').'/';
//            import('cut.Io');
//            $Io = new \Io( $filePath );
//            $data['total_num'] = $data['keep_num'] = $Io->fileCountLines( $filePath . $data['file_name'] );
//
//            $editRs = model('Bag')->isUpdate(true)->save( $data );
//            $editRs!==false ? $this->layJson(0, '重新上传礼包成功') : $this->layJson(1, '重新上传礼包失败，请重试');
//        }else{
//            $find = model('Bag')->find( input('id') );
//            return view('', ['find'=>$find]);
//        }
//    }

    //追加
    public function append(){
        if( request()->isAjax() ){
            $data = input();
            if(empty($data['file'])){
                $this->layJson(1, '请上传文件');
            }

            $find = model('Bag')->field('game_id, file_name')->where('id', $data['id'])->find();
            $gameId = $find['game_id'];
            $fileName = $find['file_name'];

            $filePath = ROOT_PATH.'uploads/bag/user/'.$gameId.'/';

            $newFile = ROOT_PATH.'uploads/bag/user/'.$gameId.'/'.$data['file'];
            import('cut.Io');
            $Io = new \Io( $filePath );
            $appendRs = $Io->addToFile($fileName, $newFile);

            if( !$appendRs['status'] ){
                $this->layJson(1, '文件不存在');
            }

            $fileNum = $Io->fileCountLines($newFile);
            $numRs1 = model('Bag')->where('id', $data['id'])->setInc('total_num', $fileNum);
            $numRs2 = model('Bag')->where('id', $data['id'])->setInc('keep_num', $fileNum);
            if( $numRs1 && $numRs2 ){
                //同步追加
                $httpRs = curl_file('http://uc.miyuhaiwai.com/Curl/uploadBag', $filePath.$fileName, $gameId);
                $dataRs = json_decode($httpRs, true);
                if($dataRs['result']==0){
                    $this->layJson(1, $dataRs['msg']);
                }

                //追加完成删除本地文件
                @unlink($newFile);

                $this->layJson(0, '追加文件成功');
            }else{
                $this->layJson(1, '追加文件失败，请重试');
            }
        }else{
            $find = model('Bag')->find( input('id') );
            $gameIdName = model('Game')->gameIdName();
            $find['game_name'] = $gameIdName[ $find['game_id'] ];
            return view('', ['find'=>$find]);
        }
    }
}